﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditPhapLuat : System.Web.UI.UserControl
    {
        DataTable dt;
        public string name;
        public string title;
        public string image;
        public string summary;
        public string description;
        public string download;
        public int id;
        public string filePathNewsIamge;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_PhapLuat", "PhapLuat_ID", id.ToString());
                name = dt.Rows[0]["PhapLuat_TieuDe"].ToString();
                title = dt.Rows[0]["PhapLuat_Title"].ToString();
                image = dt.Rows[0]["PhapLuat_Image"].ToString();
                summary = dt.Rows[0]["PhapLuat_Summary"].ToString();
                description = dt.Rows[0]["PhapLuat_Description"].ToString();
                download = dt.Rows[0]["PhapLuat_DownLoad"].ToString();
                txtName.Text = name;
                txtTitle.Text = title;
                txtDownload.Text = download;
                txtSummary.Text = summary;
                txtDescription.Text = description;

            }
            catch (Exception)
            {
                Response.Redirect("/Admin/error.htm");
            }
        }

        protected void btnUpdateSlide_Click(object sender, EventArgs e)
        {
            if (name != "")
            {
                if (fulImageNews.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImageNews.FileName))
                    {
                        LIB.Validate saveimage = new LIB.Validate();
                        filePathNewsIamge = "/Resources/Images/Images-PhapLuat/" + fulImageNews.FileName + "";
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-PhapLuat/"), fulImageNews.FileName, 200, fulImageNews.PostedFile.InputStream);
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                        cl[0] = new DAL.DHelpers.Column("PhapLuat_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[1] = new DAL.DHelpers.Column("PhapLuat_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[2] = new DAL.DHelpers.Column("PhapLuat_Image", filePathNewsIamge, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[3] = new DAL.DHelpers.Column("PhapLuat_Summary", txtSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[4] = new DAL.DHelpers.Column("PhapLuat_Description", txtDescription.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[5] = new DAL.DHelpers.Column("PhapLuat_DownLoad", txtDownload.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        string[] colWhere = new string[1];
                        colWhere[0] = "PhapLuat_ID";

                        object[] colValue = new object[1];
                        colValue[0] = id;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_PhapLuat", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=3&sub=26");
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                    cl[0] = new DAL.DHelpers.Column("PhapLuat_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[1] = new DAL.DHelpers.Column("PhapLuat_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[2] = new DAL.DHelpers.Column("PhapLuat_Summary", txtSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[3] = new DAL.DHelpers.Column("PhapLuat_Description", txtDescription.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[4] = new DAL.DHelpers.Column("PhapLuat_DownLoad", txtDownload.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                    string[] colWhere = new string[1];
                    colWhere[0] = "PhapLuat_ID";

                    object[] colValue = new object[1];
                    colValue[0] = id;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_PhapLuat", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=3&sub=26");
                }
            }
            else
            {
                Response.Write("<script>alert('Không thể để tên trống");
            }
        }
    }
}